/**
 * @file models/retailFeatureList.js
 * @author
 */

import { routerRedux } from 'dva/router';
import api from '../api';
import { delay } from '../utils/sagaEffects';

export default {
  namespace: 'retailFeatureList',
  state: {
    currentDataDate: new Date(),
    list: [],
    detail: {},
    page: {},
    featuresMapping: [],
    selectedTrader: null,
    isDetailOpen: false,
    historyAccountDailies: [],
  },
  reducers: {
    getListSuccess(state, action) {
      const { payload: { response } } = action;

      let d = state.currentDataDate;
      if (response.data != null && response.data.length > 0) {
        d = new Date(response.data[0].dataDate);
      }

      return {
        ...state,
        currentDataDate: d,
        list: response.data,
        page: response.page,
      };
    },
    getMappingSuccess(state, action) {
      const { payload: { response } } = action;
      return {
        ...state,
        featuresMapping: response.data,
      };
    },
    changeTrader(state, action) {
      const { payload: { selectedTrader } } = action;
      return {
        ...state,
        isDetailOpen: true,
        selectedTrader,
      };
    },
    changeDetailOpen(state, action) {
      const { payload: { open } } = action;
      return {
        ...state,
        selectedTrader: open?state.selectedTrader:undefined,
        isDetailOpen: open,
      };
    },
    getHistoryFeaturesSuccess(state, action) {
      const { payload: { response } } = action;
      return {
        ...state,
        historyFeatures: response.data,
      };
    },
    getHistoryAccountDailiesSuccess(state, action) {
      const { payload: { response } } = action;
      return {
        ...state,
        historyAccountDailies: response.data,
      };
    },
  },
  effects: {
    * getFeatureList({ payload: query }, { call, put }) {
      const response = yield call(api.getRetailFeatureList, query);
      yield put({
        type: 'getListSuccess',
        payload: { response },
      });
    },
    * getFeaturesMapping({ payload: query }, { call, put }) {
      const response = yield call(api.getRetailFeaturesMapping, query);
      yield put({
        type: 'getMappingSuccess',
        payload: { response },
      });
    },
    * getHistoryFeatures({ payload: query }, { call, put }) {
      const response = yield call(api.getRetailHistoryFeatures, query);
      yield put({
        type: 'getHistoryFeaturesSuccess',
        payload: { response },
      });
    },
    * getHistoryAccountDailies({ payload: query }, { call, put }) {
      const response = yield call(api.getRetailHistoryAccountDailies, query);
      yield put({
        type: 'getHistoryAccountDailiesSuccess',
        payload: { response },
      });
    },
  },
  subscriptions: {},
};
